import type { NewsArticle, Category } from '@/types'

// Mock作者数据
const mockAuthors = [
  { name: '李明', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=1', title: 'AI研究员' },
  { name: '张华', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=2', title: '区块链专家' },
  { name: '王芳', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=3', title: '硬件工程师' },
  { name: '刘强', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=4', title: '创业导师' },
  { name: '陈敏', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=5', title: '科技记者' },
  { name: '杨帆', avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=6', title: '产品经理' },
]

// Mock分类数据
export const mockCategories: Category[] = [
  { id: 'ai', name: '人工智能', icon: 'cpu', color: '#00d4ff', count: 156 },
  { id: 'blockchain', name: '区块链', icon: 'connection', color: '#39ff14', count: 89 },
  { id: 'hardware', name: '硬件', icon: 'monitor', color: '#ff6b35', count: 124 },
  { id: 'startup', name: '创业', icon: 'trend-charts', color: '#8b5cf6', count: 78 },
  { id: 'mobile', name: '移动互联', icon: 'iphone', color: '#f59e0b', count: 65 },
]

// Mock标签数据
const mockTags = [
  'ChatGPT', 'GPT-4', '机器学习', '深度学习', '神经网络', '自然语言处理',
  'Bitcoin', 'Ethereum', 'DeFi', 'NFT', 'Web3', '智能合约',
  'iPhone', 'iPad', 'MacBook', 'Apple Vision Pro', 'Tesla', 'SpaceX',
  '创业融资', '独角兽', 'IPO', '风投', 'Y Combinator', '孵化器',
  '5G', '物联网', 'AR/VR', '量子计算', '自动驾驶', '芯片'
]

// 生成随机Mock新闻数据
function generateMockArticle(id: number): NewsArticle {
  const categories: NewsArticle['category'][] = ['AI', 'Blockchain', 'Hardware', 'Startup', 'Mobile']
  const priorities: NewsArticle['priority'][] = ['breaking', 'featured', 'hot', 'normal']
  
  const category = categories[Math.floor(Math.random() * categories.length)]
  const priority = priorities[Math.floor(Math.random() * priorities.length)]
  const author = mockAuthors[Math.floor(Math.random() * mockAuthors.length)]
  
  // 根据分类生成相关标题
  const titlesByCategory = {
    AI: [
      'OpenAI发布GPT-5，性能提升300%，引领AI新时代',
      '百度文心一言4.0正式上线，挑战ChatGPT霸主地位',
      '谷歌Gemini Ultra在多项基准测试中超越GPT-4',
      'AI芯片大战升级，英伟达H100供不应求',
      '清华团队突破大模型训练瓶颈，成本降低90%',
      'Meta发布开源AI模型Llama 3，免费商用引热议',
    ],
    Blockchain: [
      'Bitcoin突破10万美元大关，市值超越黄金',
      'Ethereum 2.0完成最终升级，交易速度提升1000倍',
      '央行数字货币DCEP全面推广，覆盖100个城市',
      'DeFi协议锁仓量突破5000亿美元创历史新高',
      'Web3社交平台兴起，用户数据所有权回归个人',
      'NFT市场复苏，艺术品交易额月增长200%',
    ],
    Hardware: [
      'Apple M4芯片性能曝光，AI算力提升5倍',
      'iPhone 16 Pro发布，搭载革命性全息显示技术',
      'Tesla Model Y降价至15万，电动车进入普及时代',
      '华为Mate 70系列发布，自研芯片再创突破',
      '三星发布200MP相机传感器，手机摄影新标杆',
      'SpaceX星舰成功着陆火星，人类太空梦更进一步',
    ],
    Startup: [
      '字节跳动估值达3000亿美元，超越Meta成全球第二',
      '新能源汽车独角兽理想汽车月销量突破5万辆',
      '生物科技公司完成10亿美元C轮融资创纪录',
      'AI芯片初创公司被英伟达20亿美元收购',
      '外卖机器人公司获红杉资本5亿美元投资',
      '量子计算创业公司实现商业化突破，订单暴涨',
    ],
    Mobile: [
      '5G网络覆盖率达95%，万物互联时代正式到来',
      '折叠屏手机销量暴涨300%，成为新增长点',
      'AR眼镜出货量首次突破100万台，消费市场启动',
      '移动支付交易额突破500万亿，现金使用率降至1%',
      '短视频用户时长首超传统电视，媒体格局巨变',
      '游戏手机市场井喷，专业化设备需求激增',
    ]
  }
  
  const titles = titlesByCategory[category]
  const title = titles[Math.floor(Math.random() * titles.length)]
  
  // 生成摘要
  const summaries = [
    '这一突破性进展将彻底改变行业格局，为用户带来前所未有的体验提升。',
    '业内专家认为，这项技术的成熟将加速相关产业的数字化转型进程。',
    '市场分析师预测，该消息将对股价产生积极影响，投资者信心大增。',
    '该项目的成功实施为同类企业提供了宝贵的经验和参考模式。',
    '技术团队表示，未来还将推出更多创新功能，持续引领行业发展。'
  ]
  
  // 生成相关标签
  const categoryTags = {
    AI: ['ChatGPT', 'GPT-4', '机器学习', '深度学习', '神经网络'],
    Blockchain: ['Bitcoin', 'Ethereum', 'DeFi', 'NFT', 'Web3'],
    Hardware: ['iPhone', 'iPad', 'MacBook', 'Tesla', 'SpaceX'],
    Startup: ['创业融资', '独角兽', 'IPO', '风投', 'Y Combinator'],
    Mobile: ['5G', '物联网', 'AR/VR', '自动驾驶', '芯片']
  }
  
  const tags = categoryTags[category].slice(0, Math.floor(Math.random() * 3) + 2)
  
  return {
    id: id.toString(),
    title,
    summary: summaries[Math.floor(Math.random() * summaries.length)],
    content: `# ${title}\n\n${summaries[Math.floor(Math.random() * summaries.length)]}\n\n## 详细内容\n\n这里是文章的详细内容...`,
    category,
    tags,
    author,
    publishTime: Date.now() - Math.random() * 7 * 24 * 60 * 60 * 1000, // 最近7天内
    readCount: Math.floor(Math.random() * 50000) + 1000,
    likeCount: Math.floor(Math.random() * 5000) + 100,
    coverImage: `https://picsum.photos/800/450?random=${id}`,
    priority,
    readingTime: Math.floor(Math.random() * 10) + 3 // 3-12分钟
  }
}

// 生成Mock数据
export const mockArticles: NewsArticle[] = Array.from({ length: 50 }, (_, i) => 
  generateMockArticle(i + 1)
)

// 获取热门文章
export function getHotArticles(limit = 10): NewsArticle[] {
  return mockArticles
    .filter(article => article.priority === 'hot' || article.priority === 'featured')
    .sort((a, b) => b.readCount - a.readCount)
    .slice(0, limit)
}

// 获取最新文章
export function getLatestArticles(limit = 10): NewsArticle[] {
  return mockArticles
    .sort((a, b) => b.publishTime - a.publishTime)
    .slice(0, limit)
}

// 根据分类获取文章
export function getArticlesByCategory(category: string, limit = 20): NewsArticle[] {
  return mockArticles
    .filter(article => article.category.toLowerCase() === category.toLowerCase())
    .sort((a, b) => b.publishTime - a.publishTime)
    .slice(0, limit)
}

// 搜索文章
export function searchArticles(keyword: string, limit = 20): NewsArticle[] {
  const lowerKeyword = keyword.toLowerCase()
  return mockArticles
    .filter(article => 
      article.title.toLowerCase().includes(lowerKeyword) ||
      article.summary.toLowerCase().includes(lowerKeyword) ||
      article.tags.some(tag => tag.toLowerCase().includes(lowerKeyword))
    )
    .sort((a, b) => b.publishTime - a.publishTime)
    .slice(0, limit)
}

// 根据ID获取文章
export function getArticleById(id: string): NewsArticle | undefined {
  return mockArticles.find(article => article.id === id)
}

// 获取推荐文章（基于分类和标签）
export function getRecommendedArticles(currentArticle: NewsArticle, limit = 5): NewsArticle[] {
  return mockArticles
    .filter(article => 
      article.id !== currentArticle.id && 
      (article.category === currentArticle.category ||
       article.tags.some(tag => currentArticle.tags.includes(tag)))
    )
    .sort((a, b) => b.readCount - a.readCount)
    .slice(0, limit)
}